<?php

/**
 * This is the model class for table "student_attendance".
 *
 * The followings are the available columns in table 'student_attendance':
 * @property integer $id
 * @property integer $student_id
 * @property string $attendance_date
 * @property string $reason
 * @property integer $is_half_day
 */
class StudentAttendance extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return StudentAttendance the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'student_attendance';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('student_id, class_id, attendance_date, reason', 'required'),
			array('is_half_day', 'numerical', 'integerOnly'=>true),
			array('reason', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, student_id, attendance_date, reason, is_half_day', 'safe', 'on'=>'search'),
			array('current_date','compare','compareAttribute'=>'attendance_date','operator'=>'>=','message'=>'Invalid Attendance Date!'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'class_id' => 'Class',
			'student_id' => 'Absentee Students Number',
			'attendance_date' => 'Attendance Date',
			'reason' => 'Reason',
			'is_half_day' => 'Is Half Day',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('student_id',$this->student_id);
		$criteria->compare('attendance_date',$this->attendance_date,true);
		$criteria->compare('reason',$this->reason,true);
		$criteria->compare('is_half_day',$this->is_half_day);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function Get_class()
	{
		    $connection=Yii::app()->db;
			$class="SELECT * FROM class";
			$command=$connection->createCommand($class);
			$results=$command->query();
			return $results;
	}
	
	public function Get_Student($class_id)
	{
		    $connection=Yii::app()->db;
			$get_student="SELECT COUNT(*)as SR, COUNT(CASE WHEN gender = 'Male' THEN registration_no END) as boys, COUNT(CASE WHEN gender = 'Female' THEN registration_no END) as girls FROM registration WHERE class='$class_id'GROUP BY class";
			$command=$connection->createCommand($get_student);
			$results=$command->query();
			return $results;
	}
	public function Get_Absentee_Student($class_id,$from_date)
	{
		    $connection=Yii::app()->db;
			$get_absentee_student="SELECT student_id, class_id,attendance_date, registration_no, class, first_name,last_name,COUNT(CASE WHEN gender = 'Male' THEN registration_no END) as absentee_boys, COUNT(CASE WHEN gender = 'Female' THEN registration_no END) as absentee_girls  FROM student_attendance sa, registration sr WHERE sa.student_id=sr.registration_no and sa.class_id='$class_id' and sa.attendance_date='$from_date' GROUP BY class_id, attendance_date, class";
			$command=$connection->createCommand($get_absentee_student);
			$results=$command->query();
			return $results;
	}
	
	public function Students_absent_morethan_twodays($no_of_days,$class_id,$from_date,$to_date)
	{
		    $connection=Yii::app()->db;
			if($no_of_days=="")
			{
				$no_of_days=2;
			}
			$Students_absent_morethan_twodays="SELECT first_name, last_name,attendance_date,cell_number, registration_no,count( student_id ) AS totalabsent FROM student_attendance sa, registration sr WHERE sa.student_id=sr.registration_no and  sa.student_id = sr.registration_no and  sa.class_id='$class_id' and sa.attendance_date BETWEEN '$from_date' and '$to_date' GROUP BY student_id HAVING count( sa.student_id )>'$no_of_days'";			
			$command=$connection->createCommand($Students_absent_morethan_twodays);
			$results=$command->query();
			return $results;
	}
	
	public function Students_absent_lessthan_twodays($no_of_days,$class_id,$from_date,$to_date)
	{
		    $connection=Yii::app()->db;
			if($no_of_days=="")
			{
				$no_of_days=2;
			}
			$Students_absent_morethan_twodays="SELECT first_name, last_name,attendance_date,cell_number, registration_no,count( student_id ) AS totalabsent FROM student_attendance sa, registration sr WHERE sa.student_id=sr.registration_no and  sa.student_id = sr.registration_no and  sa.class_id='$class_id' and sa.attendance_date BETWEEN '$from_date' and '$to_date' GROUP BY student_id HAVING count( sa.student_id )>'$no_of_days'";			
			$command=$connection->createCommand($Students_absent_morethan_twodays);
			$results=$command->query();
			return $results;
	}
	
	public function get_student_attendance($keyword,$classid)
	{
		    $connection=Yii::app()->db;
			
			$get_student_attendance="SELECT * FROM registration WHERE registration_no='$keyword' and class='$classid'";
			//$get_student_attendance="SELECT first_name, last_name,attendance_date,cell_number, registration_no,count( student_id ) AS totalabsent FROM student_attendance sa, registration sr WHERE sa.student_id=sr.registration_no and  sa.student_id = sr.registration_no and  sa.class_id='$class_id' and sa.attendance_date BETWEEN '$from_date' and '$to_date' GROUP BY student_id HAVING count( sa.student_id )>'$no_of_days'";			
			$command=$connection->createCommand($get_student_attendance);
			$results=$command->query();
			return $results;
			
	}
}